#include "KeyController.h"
#include "Time.h"

KeyController::KeyController(controllers::Joystick* controller):
	DeviceController(controller),
	leftDown(false),
	rightDown(false) {
}

KeyController::~KeyController() {
}

void KeyController::run() {
	if (controller->getTrigger() && !leftDown) {
		leftDown = true;
		robot.mousePress(Robot::LEFT_BUTTON_MASK);
	}
	if (!controller->getTrigger() && leftDown) {
		robot.mouseRelease(Robot::LEFT_BUTTON_MASK);
		leftDown = false;
	}
	if (controller->getRightClick() && !rightDown) {
		rightDown = true;
		robot.mousePress(Robot::RIGHT_BUTTON_MASK);
	}
	if (!controller->getRightClick() && rightDown) {
		robot.mouseRelease(Robot::RIGHT_BUTTON_MASK);
		rightDown = false;
	}
	__int64 currTime = time::milliTime();
	__int64 elapsedTime = currTime - lastTime;
	if (controller->getScrollUp() && elapsedTime > kKeyRepeatDelay) {
		robot.mouseWheel(1);
		lastTime = currTime;
	}
	if (controller->getScrollDown() && elapsedTime > kKeyRepeatDelay) {
		robot.mouseWheel(-1);
		lastTime = currTime;
	}
}